package com.right.health.app.ui.medical;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.magic.basic.activity.BaseActivity;
import com.magic.basic.utils.ToastUtil;
import com.magics.http.listener.OnHttpListener;
import com.magics.http.model.Request;
import com.magics.http.model.Response;
import com.right.health.app.R;
import com.right.health.app.app.App;
import com.right.health.app.net.SickHistoryApi;

import static com.right.health.app.constants.BroadCastCode.REFRESH_COMMON;

/*
 * Created by hopeliao on 2019-09-30
 *
 */
public class AllergyAddActivity extends BaseActivity implements View.OnClickListener , OnHttpListener {

    private static final int HTTP_CODE_ADD = 1;

    private EditText mNameEt;

    private SickHistoryApi mSickHistoryApi;

    @Override
    public void onCreate(Bundle bundle, String s) {
        setContentView(R.layout.allergy_add);
    }

    @Override
    public void findView() {
        mNameEt = (EditText) findViewById(R.id.et_name);

        findViewById(R.id.tv_save).setOnClickListener(this);
    }

    @Override
    public void initialize() {
        mSickHistoryApi = new SickHistoryApi();
        mSickHistoryApi.setListener(this);
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.tv_save) {
            String name = mNameEt.getText().toString().trim();
            if(TextUtils.isEmpty(name)) {
                ToastUtil.show(this, mNameEt.getHint().toString().trim());
                return;
            }
            mSickHistoryApi.addRequestCode(HTTP_CODE_ADD);
            mSickHistoryApi.addAlleraySource(App.getInstance().getCurrentMember().id, name);
        }
    }

    @Override
    public void onResponse(Request request, Response response) {
        if(response.isResponseFail()) {
            ToastUtil.show(this, response.error);
        } else {
            if(request.requestCode == HTTP_CODE_ADD) {
                sendBroadcast(REFRESH_COMMON);
                ToastUtil.show(this, "添加成功");
                finish();
            }
        }
    }
}
